package org.sixdog.mcsit.service.impl;

import org.sixdog.mcsit.dao.ProblemDao;
import org.sixdog.mcsit.po.Problem;
import org.sixdog.mcsit.service.ProblemService;
import org.sixdog.mcsit.utils.HttpResult;
import org.sixdog.mcsit.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
@Service
public class ProblemServiceImpl implements ProblemService {
    @Autowired
    private ProblemDao problemDao;

    @Override
    public Problem selectProblemByProblemId(String problemId) {
        Problem problem = problemDao.selectProblemByProblemId(problemId);
        return problem;
    }

    @Override
    public HashSet<Problem> selectProblems(Problem problem, int pageNumber, int pageSize) {
        int pageStart = (pageNumber-1)*pageSize;
        List<Problem> problems = problemDao.selectProblems(problem, pageStart, pageSize);
        HashSet<Problem> resultProblems = new HashSet<>();
        if (problems.size()>0){
            problems.forEach(i -> {
                i.setCorrectOption("");
                resultProblems.add(i);
            });
        }
        return resultProblems;
    }

    @Override
    public int scoring(List<Problem> Problems) {
        int score = 0;
        Problem purifyProblem = new Problem();
        List<String> purifyProblemIds = new ArrayList<>();
        for (Problem problem: Problems) {
            if (StringUtils.isBlank(problem.getCorrectOption())) continue;
            for (String purifyProblemId : purifyProblemIds){
                if (problem.getProblemId().equals(purifyProblemId)){
                    return 0;
                }
            }
            purifyProblemIds.add(problem.getProblemId());
            purifyProblem.setProblemId(problem.getProblemId());
            purifyProblem.setCorrectOption(problem.getCorrectOption());
            List<Problem> problems = problemDao.selectProblems(purifyProblem,0,1);
            if (problems.size()==1) score++;
        }
        return score;
    }
}
